package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;
import org.json.JSONException;
/**
 *
 * @Title: Bp_Get_CGToKen
 * @Description: 宝安排水 数字化城管 获取ToKen
 * @author: chieh
 * @date 2020-07-27 12:30:00
 *
 */
public class Bp_Get_CGToKen implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(Bp_Get_CGToKen.class);
	
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {
		
		log.debug("-------------------------start Bp_Get_CGToKen 开始-----------------");
		
		//获取配置信息
		String URL = getProperty.get("SZCG.URL")+"/auth/GetToken";
		String appId = getProperty.get("SZCG.appId");
		String appSecret = getProperty.get("SZCG.appSecret");
		
		//String URL = "http://baum.baoan.gov.cn:12319/baum.webapi/api/v2/auth/GetToken";
		//String appId = "basw";
		//String appSecret = "Basw6687";

		log.debug("URL:" + URL+"	appId:" + appId+"	appSecret:" + appSecret);
		long startTime = System.currentTimeMillis();

		def reJson = "";
		try {
			//http://baum.baoan.gov.cn:12319/baum.webapi/api/v2/auth/GetToken?appId=basw&appSecret=Basw6687
			String getParam="appId="+appId+"&appSecret="+appSecret;
			String doURL =URL+ "?" + getParam;

			String getResult = Util.doHttpGet(doURL, "UTF-8", 100000, 0);
			log.debug("ToKen调用结果返回：" + getResult.toString());

			String tokenId = null;
			if(getResult!=null&&getResult.length()>0) {
				try {
					JSONObject loginObject = new JSONObject(getResult);
					String statusCode = KHUtil.GetJV(loginObject, "statusCode");
					tokenId = KHUtil.GetJV(loginObject, "result");
					String expireTime = KHUtil.GetJV(loginObject, "expireTime");
					//"expireTime": "2020-11-13 18:05:07"
					//log.debug("statusCode:"+statusCode+"	,result："+tokenId);
					eliteBean.update("update SYSC_PARAMETER set PARAMETERVALUE = '"+tokenId+"',MODIFIEDDATE='"+expireTime+"' where PARAMETER_ID = 'SZHTOK'", "dataSource");
				} catch (Exception e) {
					tokenId = null;
				}
			}			
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"BP城管获取ToKen接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":\""+tokenId+"\"}";
			
			} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"BP城管获取ToKen接口处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":{}}";
		}

		return reJson;
	}
}